package service;

import java.io.*;

/**
 * @author yk
 * @version 1.0
 * @date 2021/2/26
 */
public class Response {

    private OutputStream outputStream;

    public Response() {
    }

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    /**
     * 输出
     * @param content 内容
     */
    public void output( String content ) throws IOException {
        outputStream.write(content.getBytes());
    }

    /**
     * 输出html
     * @param path 路径
     */
    public void outputHtml( String path ) throws Exception {
        // "/E:/编程/架构师训练/tomcat及nginx的源码/tomcat源码解析/Minicat/target/classes/index.html"
        String absoluteResourcePath = StaticResourceUtil.getAbsoluteResourcePath(path);
        File file = new File(absoluteResourcePath);
        if ( file.exists() && file.isFile()){
            // 读取绝对路径，输出静态资源
            StaticResourceUtil.outputStaticResource(new FileInputStream(file),outputStream);
        }else {
            output( HttpProtocolUtil.getHttpHeader404() );
        }

    }
}
